package com.wn.service;

import com.wn.common.Const;
import com.wn.mapper.UserMapper;
import com.wn.pojo.User;
import com.wn.util.CookieUtil;
import com.wn.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    //用户的登录
    public Map<String,Object> login(User user, HttpServletResponse response){
        Map<String,Object> map= new HashMap<>();
        User login = userMapper.login(user);
        if(StringUtils.isEmpty(login)){
            map.put("code",1);
            map.put("msg","登录失败");
        }else{
            login.setPassword("");
            //生成token
            String token = JWTUtil.generateToken(login, Const.PRIVATE_KEY,30);
            //将token传入cookie并且去response
            CookieUtil.responseCookie(token,response);
            map.put("code",0);
            map.put("msg","登录成功");
        }
        return map;
    }

}
